package com.xyz.business.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.xyz.business.bean.User;
import com.xyz.business.service.UserService;
import com.xyz.constant.XYZConstant;
import com.xyz.init.infos.UserInfosInit;

/**
 * 
 * @author xyzjh
 * 
 */
public class UserHelper {

	/**
	 * 报告类型
	 */
	private static final Map<Integer, String> userTypeMap = new HashMap<Integer, String>(
			16);
	/**
	 * 日志类
	 */
	private static Logger logger = Logger.getLogger(UserHelper.class);
	static {
		userTypeMap.put(1, "注师1");
		userTypeMap.put(2, "注师2");
		userTypeMap.put(3, "联系人");
		userTypeMap.put(4, "系统用户");
		userTypeMap.put(5, "助理人员1");
		userTypeMap.put(6, "助理人员2");
		userTypeMap.put(7, "助理人员3");
		userTypeMap.put(8, "助理人员4");
		userTypeMap.put(9, "项目负责人");

	}

	public static Map<Integer, String> getUserTypeMapp() {
		return userTypeMap;
	}

	public static List<User> queryTotalUserList(UserService userService) {
		logger.info("queryTotalUserList begin....");
		List<User> totalUserList = new ArrayList<User>(32);

		try {
			// 从用户表中查询出所有用户
			totalUserList = userService.query();
			return totalUserList;

		} catch (SQLException e) {
			logger.info("catch one exception when queryUser ....");
		}
		logger.info("queryTotalUserList end....");
		return totalUserList;
	}

	public static List<User> queryUser(Integer userType,
			List<User> totalUserList) {
		logger.info("queryUser begin....");
		List<User> users = new ArrayList<User>(32);

		try {
			// // 从用户表中查询出所有用户
			// List<User> totalUserList = userService.query();
			// 根据用户类获取相应的所有的用户
			users = convertUserList(userType, totalUserList);

		} catch (Exception e) {
			logger.info("catch one exception when queryUser ....");
		}
		logger.info("queryUser end....");
		return users;
	}

	/**
	 * 根据用户类型区分用户
	 * 
	 * @param userType
	 * @param totalUserList
	 * @return 各种类型的用户列表
	 */
	private static List<User> convertUserList(Integer userType,
			List<User> totalUserList) {
		List<User> users = new ArrayList<User>(32);
		if (totalUserList != null) {
			for (User user : totalUserList) {
				if (userType.equals(user.getType())) {
					users.add(user);
				}
			}
		}

		return users;
	}

	public static void dealUser(HttpServletRequest request,
			UserService userService) {
		// List<User> totalUserList =
		// UserHelper.queryTotalUserList(userService);
		List<User> totalUserList = UserInfosInit.getUserList();
		// 分别获取各种联系人

		// 获取公司联系人下拉列表
		List<User> contact_personList = UserHelper.queryUser(
				XYZConstant.CONTACT_PERSON, totalUserList);
		// 获取项目负责人下拉列表
		List<User> project_leaderList = UserHelper.queryUser(
				XYZConstant.PROJECT_LEADER, totalUserList);

		// 获取助理人员一下拉列表
		List<User> helpmate_oneList = UserHelper.queryUser(
				XYZConstant.HELPMATE_ONE, totalUserList);
		// 获取助理人员二下拉列表
		List<User> helpmate_twoList = UserHelper.queryUser(
				XYZConstant.HELPMATE_TWO, totalUserList);
		// 获取助理人员三下拉列表
		List<User> helpmate_threeList = UserHelper.queryUser(
				XYZConstant.HELPMATE_THREE, totalUserList);
		// 获取助理人员四下拉列表
		List<User> helpmate_fourList = UserHelper.queryUser(
				XYZConstant.HELPMATE_FOUR, totalUserList);
		// 获取注师一下拉列表
		List<User> cpa_oneList = UserHelper.queryUser(XYZConstant.CPA_ONE,
				totalUserList);
		// 获取注师二下拉列表
		List<User> cpa_twoList = UserHelper.queryUser(XYZConstant.CPA_TWO,
				totalUserList);
		request.setAttribute("contact_personList", contact_personList);
		request.setAttribute("project_leaderList", project_leaderList);
		request.setAttribute("helpmate_oneList", helpmate_oneList);
		request.setAttribute("helpmate_twoList", helpmate_twoList);
		request.setAttribute("helpmate_threeList", helpmate_threeList);
		request.setAttribute("helpmate_fourList", helpmate_fourList);
		request.setAttribute("cpa_oneList", cpa_oneList);
		request.setAttribute("cpa_twoList", cpa_twoList);
		request.setAttribute("project_leaderList", project_leaderList);
	}

	/**
	 * 根据用户名查询用户编号
	 * 
	 * @param username
	 *            用户名
	 * @return
	 */
	public static String getUserNoByUserName(String username, Integer type) {
		if (!StringUtils.isBlank(username)) {
			for (User user : UserInfosInit.getUserList()) {
				if (username.equalsIgnoreCase(user.getName())
						&& type == user.getType()) {
					return user.getUid();
				}
			}
		}
		return XYZConstant.EMPTY;

	}

	/**
	 * 根据类型获取用户类型
	 * 
	 * @param type
	 * @return
	 */
	public static String getUserTypeName(Integer type) {
		return userTypeMap.get(type);
	}
}
